import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router';

const Layouts = () => import("@/layouts/index.vue")

// 导入所有router
const metaRouters = import.meta.globEager("./modules/*.js");

// 处理路由表
export const routerArray = [];

Object.keys(metaRouters).forEach(item => {
    Object.keys(metaRouters[item]).forEach(key => {
        routerArray.push(...metaRouters[item][key]);
    });
});

/**
 * @description 路由配置简介（💢没有使用动态路由，路由全部配置在本地，需要使用动态路由请自行改造）
 * @param path ==> 路由路径
 * @param name ==> 路由名称
 * @param redirect ==> 路由重定向
 * @param component ==> 路由组件
 * @param meta ==> 路由元信息
 * @param meta.requireAuth ==> 是否需要权限验证
 * @param meta.keepAlive ==> 是否需要缓存该路由
 * @param meta.title ==> 路由标题
 * @param meta.key	==> 路由key,用来匹配按钮权限
 * */

const routes = [
    {
        path: "/",
        component: Layouts,
        redirect: "/home",
        meta: {
			title: "首页",
			icon: "shouye"
		},
        children: [
            {
                path: "/home",
                component: () => import("@/views/home/index.vue"),
                name: "Home",
                meta: {
                    title: "首页",
                    icon: "shouye",
                    affix: true
                }
            }
        ]
    },
    {
        path: "/redirect",
        component: Layouts,
        meta: {
            hidden: true
        },
        children: [
            {
                path: "/redirect/:path(.*)",
                component: () => import("@/views/redirect/index.vue")
            }
        ]
    },
    {
        path: "/login",
        name: "login",
        component: () => import("@/views/login/Login.vue"),
        meta: {
            requiresAuth: false,
            hidden: true,
            title: "登录页",
            key: "login"
        }
    },
    ...routerArray,
    {
        // 找不到路由重定向到404页面
        meta: {
            hidden: true
        },
        path: "/:pathMatch(.*)",
        redirect: { name: "404" }
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    // history: createWebHistory(),
    routes,
    strict: false,
    // 切换页面，滚动到最顶部
    scrollBehavior: () => ({ left: 0, top: 0 })
});

export default router;